package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.PageSearchDTO;
import com.qf.entity.MallGoodsBrand;
import com.qf.mapper.GoodsBrandMapper;
import com.qf.service.IGoodsBrandService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>title: com.qf.service.impl</p>
 * <p>Company: wendao</p>
 * author zhuximing
 * date 2021/9/15
 * description:
 */
@Service
public class GoodsBrandService implements IGoodsBrandService {

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Override
    public PageResult<MallGoodsBrand> selectByPage(PageSearchDTO<MallGoodsBrand> pageSearchDTO) {

        if (pageSearchDTO == null) {
            return  new PageResult<>(false, "参数不合法");
        }

        //设置分页信息
        PageHelper.startPage(pageSearchDTO.getPage(), pageSearchDTO.getSize());

        List<MallGoodsBrand> byPage = goodsBrandMapper.findByPage( pageSearchDTO.getWhere());

        //获取分页信息
        PageInfo<MallGoodsBrand> pageInfo = new PageInfo<>(byPage);

        PageResult<MallGoodsBrand> pageResult = new PageResult<>(true,"success");

        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());



        return pageResult;
    }

    @Override
    public Result save(MallGoodsBrand brand) {

        if (brand == null) {
            return  new Result(false, "参数不合法");
        }

        brand.setShowStatus(0);

        int save = goodsBrandMapper.save(brand);

        return new Result(save==1?true:false, save==1?"success":"fail");
    }

    @Override
    public Result update(MallGoodsBrand brandEdit) {
        if (brandEdit == null) {
            return  new Result(false, "参数不合法");
        }
        int update = goodsBrandMapper.update(brandEdit);

        return new Result(update==1?true:false, update==1?"success":"fail");
    }

    @Override
    public Result delete(long id) {
        if (id == 0) {
            return  new Result(false, "删除失败");
        }

        int delete = goodsBrandMapper.delete(id);

        return new Result(delete==1?true:false, delete==1?"success":"fail");


    }

    @Override
    public Result deleteList(List<MallGoodsBrand> brandList) {
        if (brandList == null || brandList.size()==0) {
            return  new Result(false, "删除失败");
        }

        int i = goodsBrandMapper.deleteList(brandList);
        return new Result(i != 0?true:false, i!=0?"success":"fail");

    }
}